package cn.kevenpotter.leetcode.easy.e01;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TwoSum {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum(new int[]{2, 7, 11, 15}, 9)));
    }

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            int cha = target - nums[i];
            if (m.containsKey(cha)) {
                return new int[]{m.get(cha), i};
            }
            m.put(nums[i], i);
        }
        throw new RuntimeException("No Answer!");
    }
}
